java SQL未命名参数语法
我查了两次为什么会这样?我有正确数量的未命名参数,列名称正确
*
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? , ? , ? , ? ,?,? )' at line 1
public void adiciona(Libro libro) {
try {
String sql = "insert into libro (isbn, titulo, precio, stock, cod_categoria, cod_editorial) values ( ? , ? , ? , ? ,?,? )";
String sqlQuery = "select count(*) from libro where isbn = " + libro.getIsbn();
sentencia = connection.createStatement();
resultSet = sentencia.executeQuery(sqlQuery);
resultSet.next();
if (resultSet.getInt(1) == 0) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, libro.getIsbn());
preparedStatement.setString(2, libro.getTitulo());
preparedStatement.setDouble(3, libro.getPrecio());
preparedStatement.setInt(4, libro.getStock());
preparedStatement.setInt(5, libro.getCod_categoria());
preparedStatement.setInt(6, libro.getCod_editorial());
retorno = sentencia.executeUpdate(sql);
preparedStatement.execute();
} catch (SQLIntegrityConstraintViolationException e) {
System.out.printf("error duplicado: %s\n",e);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}else if(retorno> 0) {System.out.println("added");
}else { System.out.println("no added.");}
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
# 1 楼答案
你可能编辑了问题,尝试了一些东西。一个干净的版本是:
# 2 楼答案
在
retorno = sentencia.executeUpdate(sql);
尝试删除参数sql